<template>
  <!-- 分销通知 -->
  <div class="page-distribution-auditing">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionBind.noticetitle') + ':'">
            <el-input
              v-model="searchForm.msgTitle"
              type="text"
              clearable
              :placeholder="$t('distributionBind.noticetitle')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 操作栏 -->
      <div class="operation-bar">
        <div
          v-if="isAuth('distribution:distributionMsg:save')"
          class="default-btn primary-btn"
          @click.stop="onAddOrUpdate()"
        >
          {{ $t("seckill.newAdd") }}
        </div>
        <el-button
          v-if="isAuth('distribution:distributionMsg:delete')"
          :disabled="!dataListSelections.length"
          @click.stop="onDelete"
        >
          {{ $t('remindPop.batchDeletion') }}
        </el-button>
      </div>
      <!-- 表格 -->
      <div class="table-con distribution-msg-table">
        <el-table
          ref="distributionMsgTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @selection-change="selectionChange"
        >
          <el-table-column
            type="selection"
            width="60"
          />
          <!-- 通知标题 -->
          <el-table-column
            prop="msgTitle"
            :label="$t('distributionBind.noticetitle')"
            width="500"
          >
            <template #default="scope">
              {{ scope.row.msgTitle }}
            </template>
          </el-table-column>
          <!-- 生效时间 -->
          <el-table-column
            min-width="160"
            prop="startTime"
            :label="$t('marketing.castTime')"
            sortable
          >
            <template #default="scope">
              {{ scope.row.startTime }}
            </template>
          </el-table-column>
          <!-- 失效时间 -->
          <el-table-column
            min-width="160"
            prop="endTime"
            :label="$t('marketing.endTime')"
            sortable
          >
            <template #default="scope">
              {{ scope.row.endTime }}
            </template>
          </el-table-column>
          <!-- 操作人 -->
          <el-table-column
            prop="username"
            :label="$t('distribUserWallet.operator')"
          >
            <template #default="scope">
              {{ scope.row.sysUser ? scope.row.sysUser.username : '' }}
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="150"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('distribution:distributionMsg:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.msgId)"
                >
                  {{ $t('coupon.edit') }}
                </div>
                <div
                  v-if="isAuth('prod:prod:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row)"
                >
                  {{ $t('remindPop.delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddOrUpdate from './add-or-update.vue'

onMounted(() => {
  getDataList()
})
const dataListSelections = ref([])
const selectAll = ref(false)
/**
 * 多选回调
 * @param list
 */
const selectionChange = (list) => {
  dataListSelections.value = list
  selectAll.value = list.length === dataList.value.length
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param msgId
 */
const onAddOrUpdate = (msgId) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(msgId)
  })
}

/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

/**
 *  刷新回调
 */
const refreshChange = () => {
  page.currentPage = 1
  getDataList(page)
}

let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const dataList = ref([])
const dataListLoading = ref(false)
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (page) {
    const size = Math.ceil(page.total / page.pageSize)
    page.currentPage = (page.currentPage > size ? size : page.currentPage) || 1
  }
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/distribution/distributionMsg/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : page.currentPage,
      size: pageParam ? pageParam.pageSize : page.pageSize
    }, tempSearchForm))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false
    })
}

/**
 * 删除
 * @param row
 */
const onDelete = (row) => {
  const ids = row.msgId ? [row.msgId] : dataListSelections.value.map(item => {
    return item.msgId
  })
  if (!row.msgId && !dataListSelections.value.length) {
    return
  }
  // `确定进行[${row.msgId ? '删除' : '批量删除'}]操作?`
  let tip = $t('remindPop.makeSure') + '[' + (row.msgId ? $t('remindPop.delete') : $t('remindPop.batchDeletion'))
  tip = tip + ']' + $t('remindPop.operation') + '?'
  ElMessageBox.confirm(tip, $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl('/distribution/distributionMsg'),
        method: 'delete',
        data: http.adornData(ids, false)
      })
        .then(() => {
          page.total = page.total - ids.length
          ElMessage({
            message: $t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              refreshChange()
            }
          })
        })
    })
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
</script>

<style lang="scss" scoped>
.page-distribution-auditing {
  :deep(.separator) {
    line-height: 2.5;
    display: inline-block;
    margin-right: 8px;
  }

  .default-btn {
    margin-right: 10px;
  }
}
</style>
